package com.huawei.cloud.phone.management.domain.phone;

import com.huawei.cloud.phone.management.common.exception.InvalidParamException;
import com.huawei.cloud.phone.management.common.exception.PhoneNotManagerException;
import com.huawei.cloud.phone.management.common.exception.ServerNotManagerException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@AllArgsConstructor
@Component
public class PhoneDomainService {
    private final PhoneGateway phoneGateway;

    /**
     * 同步管理面手机信息
     *
     * @param serverId 手机服务器ID
     */
    public void syncPhonesFromManager(String serverId) {
        List<Phone> phonesFromManager = phoneGateway.getPhonesFromManager(serverId);
        if (phonesFromManager.isEmpty()) {
            throw new InvalidParamException(String.format("服务器 %s 不存在", serverId));
        }
        phoneGateway.savePhones(phonesFromManager);
    }

    /**
     * 同步管理面手机信息
     *
     * @param phoneId 手机ID
     */
    public void syncPhoneFromManager(String phoneId) {
        Optional<Phone> phoneFromManager = phoneGateway.getPhoneFromManager(phoneId);
        if (!phoneFromManager.isPresent()) {
            throw new InvalidParamException(String.format("手机 %s 不存在", phoneId));
        }
        phoneGateway.savePhones(Collections.singletonList(phoneFromManager.get()));
    }

    /**
     * 维护超时没有收到心跳的云手机的可用状态
     *
     * @param timeout 超时时间，单位秒
     */
    public void syncPhoneUseStatus(long timeout) {
        phoneGateway.updatePhoneUseStatus(PhoneUseStatus.FAULT, timeout);
    }

    /**
     * 修改云手机的可使用状态
     *
     * @param phoneId   云手机ID
     * @param useStatus 云手机可使用状态
     */
    public void updatePhoneUseStatus(String phoneId, PhoneUseStatus useStatus) {
        phoneGateway.updatePhoneUseStatus(phoneId, useStatus);
    }

    public void checkServerIds(List<String> serverIds) {
        List<String> notManagerServerIds = phoneGateway.getNotManagerServerId(serverIds);
        if (!notManagerServerIds.isEmpty()) {
            throw new ServerNotManagerException(notManagerServerIds.get(0));
        }
    }

    public void checkPhoneIds(List<String> phoneIds) {
        List<String> notManagerPhoneIds = phoneGateway.getNotManagerPhoneId(phoneIds);
        if (!notManagerPhoneIds.isEmpty()) {
            throw new PhoneNotManagerException(notManagerPhoneIds.get(0));
        }
    }

    /**
     * 根据服务器ID和手机ID,获取所涉及的所有服务器ID和手机ID,并返回所有的服务器ID和手机ID
     *
     * @param serverIds 服务器ID
     * @param phoneIds  手机ID
     * @return 服务器ID和所属手机ID
     */
    public Map<String, Set<String>> getServerAndPhoneIds(List<String> serverIds, List<String> phoneIds) {
        List<Phone> phones = new ArrayList<>(phoneGateway.getPhonesByServerIds(serverIds));
        phones.addAll(phoneGateway.getPhonesByPhoneIds(phoneIds));
        return phones.stream().collect(Collectors.groupingBy(Phone::getServerId,
                Collectors.mapping(Phone::getPhoneId, Collectors.toSet())));
    }

    /**
     * 给用户新增手机
     *
     * @param userPhone 用户手机
     */
    public void addUserPhone(UserPhone userPhone) {
        phoneGateway.addUserPhones(Collections.singletonList(userPhone));
    }

    /**
     * 查询用户所有手机
     *
     * @param userId 用户ID
     * @return 所有用户手机
     */
    public List<UserPhone> getPhones(String userId) {
        return phoneGateway.getUserPhones(userId);
    }

    /**
     * 校验手机ID是否属于该用户
     *
     * @param userId      用户ID
     * @param userPhoneId 用户手机ID
     * @return 手机是否属于该用户
     */
    public boolean isUserPhone(String userId, String userPhoneId) {
        List<UserPhone> phones = getPhones(userId);
        return phones.stream().map(UserPhone::getPhoneId).anyMatch(userPhoneId::equals);
    }
}
